<template>
  <div>
    <h1>投诉管理</h1>
    <el-row :gutter="20">
      <el-col :span="7" :offset="5">
        <div class="block">
          <el-date-picker
              style="width: 170px"
              v-model="startTime"
              type="date"
              :picker-options="startTimePickerOptions"
              placeholder="起始日期">
          </el-date-picker> -
          <el-date-picker
              style="width: 170px"
              v-model="endTime"
              type="date"
              :picker-options="endTimePickerOptions"
              placeholder="结束日期">
          </el-date-picker>
        </div>
      </el-col>
      <el-col :span="2">
        <el-select v-model="searchDate.processingStatus" placeholder="请选择状态">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-col>

      <el-col :span="2">
        <el-button @click="handleSearch()" type="primary" icon="el-icon-search">搜索</el-button>
      </el-col>
      <el-col :span="2">
        <el-button type="info" icon="el-icon-refresh">刷新</el-button>
      </el-col>

    </el-row>
    <br>
    <el-row>
      <el-col :span="22" :offset="2">
        <el-table
            :data="tableData"
            border
            style="width: 90%">
          <el-table-column
              :resizable="false"
              fixed
              prop="id"
              label="投诉编号"
              width="80">
          </el-table-column>
          <el-table-column
              :resizable="false"
              prop="complaintTime"
              label="投诉时间"
              width="200">
          </el-table-column>
          <el-table-column
              :resizable="false"
              prop="resident.name"
              label="投诉人姓名"
              width="120">
          </el-table-column>
          <el-table-column
              :resizable="false"
              prop="complaintLocation"
              label="位置描述"
              width="200">
          </el-table-column>
          <el-table-column
              :resizable="false"
              prop="resident.phone1"
              label="投诉人联系方式"
              width="150">
          </el-table-column>
          <el-table-column
              :resizable="false"
              prop="processingStatus"
              label="处理状态"
              width="100">
          </el-table-column>
          <el-table-column
              :resizable="false"
              fixed="right"
              label="操作"
              width="150">
            <template slot-scope="scope">
              <el-button @click="viewDetails(scope.row.id)" type="primary" plain size="small">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row >
          <el-col :span="11" :offset="5">
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page.sync="pageNum"
                :page-size="pageSize"
                layout="total, prev, pager, next"
                :total="total">
            </el-pagination>
          </el-col>
        </el-row>

      </el-col>
    </el-row>


    <el-dialog
        title="查看"
        :visible.sync="dialogVisible"
        width="50%"
        center>
      <el-form>
        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="投诉人:">
              {{resident.name}}
<!--              <el-input style="width: 100px" v-model="resident.name" :disabled="true"></el-input>-->
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="2">
            <el-form-item label="投诉人联系方式:">
              <div>{{resident.phone1}}</div>
<!--              <el-input style="width: 150px" v-model="" :disabled="true"></el-input>-->
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="10">
            <el-form-item label="位置描述:">
              <div>{{complaintInfo.complaintLocation}}</div>
<!--              <el-input style="width: 200px" v-model="complaintInfo.complaintLocation" :disabled="true"></el-input>-->
            </el-form-item>
          </el-col>

          <el-col :span="10">
            <el-form-item label="投诉时间:">
              <div>{{complaintInfo.complaintTime}}</div>
<!--              <el-input style="width: 200px" v-model="complaintInfo.complaintTime" :disabled="true"></el-input>-->
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="22">
            <el-form-item label="投诉内容:">
              <br>
              <div style="width: 550px">{{complaintInfo.complaints}}</div>
<!--              <el-input type="textarea" style="width: 550px" :rows="5" v-model="complaintInfo.complaints" :disabled="true"></el-input>-->
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="22">
            <el-form-item label="投诉回复:">
              <el-input type="textarea" resize="none" style="width: 550px" :rows="5" v-model="complaintInfo.feedback"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="24">
          <el-col :span="8">
            <el-form-item label="受理人:">
              <div v-if="complaintInfo.processingStatus=='0'">
                <el-select style="width: 100px" v-model="value" placeholder="请选择">
                  <el-option
                      v-for="staffs in staffs"
                      :key="staffs.id"
                      :label="staffs.employeeName"
                      :value="staffs.id">
                  </el-option>
                </el-select>
              </div>
              <div v-else>
                {{ staff.employeeName }}
              </div>
            </el-form-item>
          </el-col>

          <el-col :span="14">
            <div v-if="complaintInfo.processingStatus=='0'">
              <el-form-item label="请选择预估处理时间:">
                <el-date-picker
                    v-model="processingTime"
                    type="date"
                    placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </div>
            <div v-else>
              <el-form-item label="预计处理时间:">
                {{ complaintInfo.processingTime }}
              </el-form-item>
            </div>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="buttonDisabled" @click="updateComplaint">确 定</el-button>

      </div>
    </el-dialog>

  </div>
</template>

<style scoped>

</style>
<script>
export default {
  data() {
    return {
      tableData: [{
        id: '',
        residentId: '',
        complaintLocation: '',
        complaints: '',
        complaintTime: '',
        staffId: '',
        processingTime: '',
        processingStatus: '',
        resident: {},
        staff: {},
      }],
      options: [{
        value: '0',
        label: '未处理'
      }, {
        value: '1',
        label: '已处理'
      }],
      radio: '',
      feedback: '',
      complaintInfo: {},
      dialogVisible: false,
      searchDate: {
        processingStatus:'',
      },
      processingTime: '',
      value: '',
      startTime: "",
      endTime: "",
      resident: {},
      staff: {},
      staffs: [],
      buttonDisabled:false,

      pageSize:null,
      pageNum:null,
      total:null,
      startTimePickerOptions:{
        disabledDate:(date)=>{
          if(this.endTime)
          {
            return date>this.endTime;
          }
          else{
            return false;
          }
        }
      },
      endTimePickerOptions:{
        disabledDate:(date)=>{
          return date<this.startTime;
        }
      },

    }
  },
  created() {
    this.handleSearch();
    this.getAllStaffs();
  },
  methods: {
    handleCurrentChange(val){
      this.pageNum = val;
      this.handleSearch();
    },
    getAllComplaint() {
      this.$axios.post("manage/selectComplaint")
          .then(response => {
            for (let i = 0; i < response.data.data.length; i++) {
              if (response.data.data[i].processingStatus != 1) {
                // console.log(response.data.data[i].processingStatus);
                response.data.data[i].processingStatus = "未处理";
              } else if (response.data.data.processingStatus = 1) {
                response.data.data[i].processingStatus = "已处理";
              }
            }
            this.tableData = response.data.data;
          })
    },
    viewDetails(id) {
      this.dialogVisible = true;
      this.$axios.get("manage/selectComplaintById", {params: {id: id}})
          .then(response => {
            this.complaintInfo = response.data.data;
            this.resident = response.data.data.resident;
            this.staff = response.data.data.staff;
            // console.log(this.staff)
            if (this.complaintInfo.processingStatus=='1'){
              this.buttonDisabled=true;
            }else {
              this.buttonDisabled=false;
            }
            console.log(this.complaintInfo);
          })
    },
    handleSearch() {
      this.$axios.post("manage/searchComplaintByCondition",this.searchDate,{params:{pageNum:this.pageNum,startTime:this.startTime,endTime:this.endTime}})
          .then(response=>{
            console.log(response);
            for (let i = 0; i < response.data.data.list.length; i++) {
              if (response.data.data.list[i].processingStatus != 1) {
                response.data.data.list[i].processingStatus = "未处理";
              } else if (response.data.data.list[i].processingStatus = 1) {
                response.data.data.list[i].processingStatus = "已处理";
              }
            }
            this.tableData = response.data.data.list;
            this.total = response.data.data.total;
            this.pageSize = response.data.data.pageSize;
          });
    },
    getAllStaffs() {
      this.$axios.get("manage/selectStaff")
          .then(response => {
            this.staffs = response.data.data;
            console.log(this.staffs);
          })
    },
    updateComplaint() {
      this.complaintInfo.staffId = this.value;
      this.complaintInfo.processingTime = this.processingTime;
      console.log(this.complaintInfo);
      if (this.complaintInfo.staffId===""&&this.complaintInfo.processingTime===""&&this.complaintInfo.feedback==null){
        this.$message({
          showClose: true,
          message: '有未填写的信息！',
          type: 'error',
        });
        return;
      }
      this.$axios.post("manage/updateComplaint",this.complaintInfo)
          .then(response=>{
            this.value='';
            this.processingTime='';
            this.$message({
              showClose: true,
              message: '处理完成',
              type: 'success'
            });
            this.dialogVisible = false;
            this.handleSearch();
          })
    }

  }
}
</script>
